package org.brownflat.lighthouse.monitoring.device.delcom;

import org.apache.log4j.Logger;
import org.brownflat.lighthouse.monitoring.action.delcom.DelcomAction;
import org.brownflat.lighthouse.monitoring.device.Device;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Implementation for the Delcom build light.
 */
public class DelcomBuildLight implements Device<DelcomAction> {

    private String executable;
    private volatile String lighthouseHome;
    private AtomicBoolean isOn = new AtomicBoolean(Boolean.FALSE);
    private static final Logger LOG = Logger.getLogger(DelcomBuildLight.class);

    public DelcomBuildLight(String executable) {
        lighthouseHome = System.getenv("LIGHTHOUSE_HOME");
        this.executable = lighthouseHome + "/" + executable;
    }

    public void execute(DelcomAction action) {
        LOG.debug("Action executing: " + action.getActionDescription());
        Runtime run = Runtime.getRuntime();
        isOn.set(action.turnsOn());
        try {
            run.exec(executable + " " + action.command());
        } catch (IOException e) {
            LOG.error(e);
        }

        //this device needs a couple of seconds to absorb the command
        //before the next one, so pause here.
        try {
            Thread.sleep(1500l);
        } catch (InterruptedException e) {
            LOG.error(e);
        }
    }
}
